package leetcode;

public class PlusOne {

	public int[] plusOne(int[] digits) {
		if (digits == null) {
			return null;
		}
		int[] temp = new int[digits.length];
		int[] res = new int[digits.length + 1];
		int carry = 0;
		temp[digits.length - 1] = (digits[digits.length - 1] + 1) % 10;
		carry = (digits[digits.length - 1] + 1) / 10;
		for (int i = digits.length - 2; i >= 0; i--) {
			temp[i] = (digits[i] + carry) % 10;
			carry = (digits[i] + carry) / 10;
		}
		if (carry == 0) {
			return temp;
		} else {
			for (int i = digits.length; i >= 1; i--) {
				res[i] = temp[i - 1];
			}
			res[0] = carry;
		}
		return res;
	}
}
